package com.yuchen.ishop.web.app.buyer;

import com.yuchen.ishop.Gobals;
import com.yuchen.ishop.entity.Buyer;
import com.yuchen.ishop.service.buyer.BuyerRestClient;
import com.yuchen.ishop.vo.CookieUserInfo;
import com.yuchen.ishop.web.BaseAction;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/my/buyer")
public class BuyerAction extends BaseAction{

	@Autowired
	BuyerRestClient buyerRestClient;
	
	@RequestMapping(value = "index")
    public String index(HttpServletRequest request, HttpServletResponse response) {
        return "my/buyer/index";
    }

    @RequestMapping(value = "buyerInfo")
    public String buyerInfo(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        CookieUserInfo userInfo = cookie2UserInfo(request);
        Buyer buyer = buyerRestClient.getById(userInfo.getId());
        modelMap.addAttribute("buyer",buyer);
        modelMap.addAttribute("myMenu", "buyerInfo");
        return "my/buyer/buyerInfo";
    }

    @RequestMapping(value = "saveOrUpdateBuyer",method = RequestMethod.POST)
    public String saveOrUpdateBuyer(Buyer buyer, HttpServletRequest request,RedirectAttributes redirectAttributes) {
        if(buyer.getId()!=null){
            buyerRestClient.update(buyer);
            redirectAttributes.addFlashAttribute(Gobals.MSG,successJson());
            return "redirect:/my/buyer/buyerInfo.htm";
        }
        return "redirect:/error.htm";
    }

    @RequestMapping(value = "buyerPwd")
    public String buyerPwd(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
        CookieUserInfo userInfo = cookie2UserInfo(request);
        if("buyer".equals(userInfo.getRole())){
            modelMap.addAttribute("myMenu", "password");
            Buyer buyer = buyerRestClient.getById(userInfo.getId());
            modelMap.put("buyerId", buyer.getId());
            return "/my/buyer/buyerPwd";
        }
        return "redirect:/error.htm";
    }

    @RequestMapping(value = "updateBuyerPwd",method = RequestMethod.POST)
    public String updateBuyerPwd(Long id,String password, HttpServletRequest request,RedirectAttributes redirectAttributes) {
        if(id!=null && StringUtils.isNotBlank(password)){
            Map map = new HashMap(2);
            map.put("id",id);
            map.put("password",password);
            buyerRestClient.updatePwd(map);
            redirectAttributes.addFlashAttribute(Gobals.MSG, successJson());
            return "redirect:/my/buyer/buyerPwd.htm";
        }
        return "redirect:/error.htm";
    }
}
